<?php
namespace Handlers;

class YopurmaqHandler extends Handler
{
    const VERSION = 'v2';
    protected $database = 'yopurmaq';
    protected $table = 'information';

    public $fields = [
        'id' => 'int',
        'uuid' => 'string',
        'title' => 'string', 
        'thumb' => 'string', 
        'video' => 'int', 
        'created_at' => 'datetime', 
        'updated_at' => 'datetime', 
        'changed_at' => 'datetime',
        'deleted' => 'int',
        'status' => 'int',
        'category_id' => 'int',
        'created_fans_id' => 'int',
        'created_memeber_id' => 'int',
        'city_id' => 'int',
        'district_id' => 'int',
        'created_individual_member_id' => 'int',
        'buy_status' => 'int',
        'edit' => 'int',
        'content' => 'json', 
    ];

    public function insertOrUpdate(array $data, $tableId = 0, $insertRawData = false) {

        $id = (int)$data['id'];
        $category_id = $data['category_id'];

        $info = $this->fetchInfoFromMysql($id);

        $params = [
            'index' => self::VERSION . '_' . $this->database . '_' . $this->table . '_' . $category_id,
            'id' => $id,
            'type' => '_doc',
            'body' => $info
        ];

        $this->log('info', 'input:', $params);
        
        try {
            $res = $this->getEs()->index($params);
        } catch (\Exception $t) {
            $this->log('error', 'insert or update exception: ' . $t->getMessage());
            $this->log('error', 'ERROR DETAIL: ' . var_export($data, true) );
            return ;
        }
        
        $this->log('info', 'result: ' . $res['result']);
    }

    public function delete(array $data, $tableId = 0) {
        
        $id = (int)$data['id'];
        $category_id = $data['category_id'];

        $params = [
            'index' => self::VERSION . '_' . $this->database . '_' . $this->table . '_' . $category_id,
            'type' => '_doc',
            'id' => $id
        ];

        $this->log('info', 'input:' . json_encode($params));
        
        try {
            $res = $this->getEs()->delete($params);
        } catch (\Exception $t) {
            $this->log('error', 'delete exception: ' . $t->getMessage());
            $this->log('error', 'ERROR DETAIL: ' . var_export($data, true) );
            return ;
        }
        $this->log('info', 'result: ' . $res['result']);
    }

    public function formatData($columns) {
        $fieldNames = array_keys($this->fields);
        
        $arr = [];

        foreach ($columns as $column) {
            $name = $column->getName();
            $value = $column->getValue();

            if (! in_array($name, $fieldNames)) {
                continue;
            }

            $value = $this->changeFieldValueType($name, $value);

            $arr[$name] = $value;
        }

        // 校正数据
        if (isset($arr['content'])) {
            $arr['content'] = $this->contentCorrection($arr['content']);
        }

        return (array)$arr;
    }

    public function formatMysqlData(array $row) { 

        $fieldNames = array_keys($this->fields);
        
        $arr = [];

        foreach ($row as $name => $value) {

            if (! in_array($name, $fieldNames)) {
                continue;
            }

            $value = $this->changeFieldValueType($name, $value);

            $arr[$name] = $value;
        }

        // 校正数据
        if (isset($arr['content'])) {
            $arr['content'] = $this->contentCorrection($arr['content']);
        }

        return (array)$arr;
    }

    function contentCorrection(array $data) {
        unset($data['content']->_mobile);
        unset($data['content']->_qrcode);

        if (isset($data['bahasi'])) {
            $data['bahasi'] = (int)$data['bahasi'];
        }

        if (isset($data['kulimi'])) {
            $data['kulimi'] = (int)$data['kulimi'];
        }

        return $data;
    }

    function fetchInfoFromMysql($id) {

$sql = <<<EOD
select 
information.id, 
information.uuid, 
updated_at, 
changed_at, 
deleted, 
status, 
category_id, 
city_id, 
district_id, 
created_individual_member_id, 
buy_status, 
title, 
thumb, 
video, 
information_content.content
from information, information_content 
where information.id = ? and information.uuid = information_content.uuid
EOD;
        $row = $this->connect->fetch($sql, $id);

        return $this->formatMysqlData((array)$row);
    }

}
